<!-- 部门树 -->
<template>
  <el-card shadow="never">
    <el-input v-model="deptName" placeholder="部门名称" clearable>
      <template #prefix>
        <i-ep-search />
      </template>
    </el-input>

    <el-tree
      ref="deptTreeRef"
      class="fix-item mt-2"
      :data="deptList"
      :props="{ children: 'children', label: 'name', disabled: '' }"
      style="--fix-top-height: 80px; overflow-y: auto"
      :expand-on-click-node="false"
      :filter-node-method="handleFilter"
      default-expand-all
      @node-click="handleNodeClick"
    />
  </el-card>
</template>

<script setup lang="ts">
import { listDepts } from "@/api/dept";
import { DeptVO } from "@/api/dept/types";

const props = defineProps({
  modelValue: {
    type: [String],
    default: undefined,
  },
});

const deptList = ref<TreeResult<DeptVO>[]>(); // 部门列表
const deptTreeRef = ref(ElTree); // 部门树
const deptName = ref(); // 部门名称

const emits = defineEmits(["node-click"]);

// 当检测到modelValue发生变化，自动触发this.$emit('update:modelValue', newValue) 向上传递
// useVModel函数代理了父子之间的数据交互
const deptId = useVModel(props, "modelValue", emits);

watchEffect(
  () => {
    deptTreeRef.value.filter(deptName.value);
  },
  {
    flush: "post", // watchEffect会在DOM挂载或者更新之前就会触发，此属性控制在DOM元素更新后运行
  }
);

/** 部门筛选 */
function handleFilter(value: string, data: any) {
  if (!value) {
    return true;
  }

  return data.name.indexOf(value) !== -1;
}

/** 部门树节点 Click */
function handleNodeClick(data: { [key: string]: any }) {
  deptId.value = data.id;
  emits("node-click");
}

onBeforeMount(() => {
  listDepts().then((res) => {
    deptList.value = res.data;
  });
});
</script>
